﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class SysMenuItemListDAL : ISysMenuItemListDAL
    {
       IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public SysMenuItemListDAL()
        {

        }

        public IList<SysMenuItemListInfo> LoadMenuByUsername(int username)
        {        
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr     = null;
            IList<SysMenuItemListInfo> infoList = new List<SysMenuItemListInfo>();
            try
            {
           
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@username", username);
                dbManager.AddParameters(2, "@SystemID", "GHRMS");

                strSQL = "SELECT DISTINCT ml.MENU_ITEM_SEQ"
                        + ", CONCAT('<div class=font-menu>', ml.MENU_ITEM_NAME, '</div>') AS MENU_ITEM_NAME"
                        + ", ml.UPPER_MENU_ITEM_SEQ"
                        + ", ml.MENU_ITEM_PATH"
                        + ", ml.MENU_ITEM_TARGET"
                        + ", ml.RECORD_STATUS"
                        + ", ml.MENU_ITEM_CODE"
                    + " FROM SYS_SC_USERS u"
                    + " INNER JOIN SYS_SC_USER_ROLES ur ON u.USER_SEQ = ur.USER_SEQ"
                        + " INNER JOIN SYS_SC_ROLES r ON ur.ROLE_SEQ = r.ROLE_SEQ"
                        + " INNER JOIN SYS_SC_PERMISSIONS p ON r.ROLE_SEQ = p.ROLE_SEQ"
                        + " INNER JOIN SYS_MENU_ITEMS_LIST ml ON p.MENU_ITEM_SEQ = ml.MENU_ITEM_SEQ"
                    + " WHERE u.RECORD_STATUS not like @DeleteRecord"
                        + " AND ur.RECORD_STATUS not like @DeleteRecord"
                        + " AND r.RECORD_STATUS not like @DeleteRecord"
                        + " AND p.RECORD_STATUS not like @DeleteRecord"
                        + " AND ml.RECORD_STATUS not like @DeleteRecord"
                        + " AND u.USER_SEQ = @username"
                        + " AND ml.SYSTEM_ID = @SystemID"
                        + " ORDER BY ml.MENU_ITEM_CODE"
                        ;

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
           
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        SysMenuItemListInfo info = new SysMenuItemListInfo();
                        info.Seq = Convert.ToInt32(dr[i]["MENU_ITEM_SEQ"]);
                        info.Name = (dr[i]["MENU_ITEM_NAME"]).ToString();
                        info.UpperSeq = (Convert.IsDBNull(dr[i]["UPPER_MENU_ITEM_SEQ"]) ? null : (Nullable<int>)(dr[i]["UPPER_MENU_ITEM_SEQ"]));
                        info.Path = (dr[i]["MENU_ITEM_PATH"]).ToString();
                        info.Target = (dr[i]["MENU_ITEM_TARGET"]).ToString();
                        info.RecordStatus = (dr[i]["RECORD_STATUS"]).ToString();

                        infoList.Add(info);
                    }
               
                }
            
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
             //   dbManager.Close(); 
              dbManager.Dispose();
            }

            return infoList;
        }
        public IList<SysMenuItemListInfo> LoadWebSiteMap(int username)
        {

          //  IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<SysMenuItemListInfo> infoList = new List<SysMenuItemListInfo>();
            try
            {
 
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@username", username);

                strSQL = "SELECT MENU_ITEM_SEQ, MENU_ITEM_CODE, MENU_ITEM_NAME, UPPER_MENU_ITEM_SEQ"
                            + ", MENU_ITEM_PATH, MENU_ITEM_TARGET, RECORD_STATUS"
                            + ", NULL AS DESCRIPTION"
                            + ", NULL AS IMAGE_URL"
                          + " FROM SYS_MENU_ITEMS_LIST"
                          + " WHERE RECORD_STATUS not like @delete_status"
                          + " ORDER BY MENU_ITEM_CODE";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
   
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        SysMenuItemListInfo info = new SysMenuItemListInfo();
                        info.Seq = Int32.Parse(dr[i]["MENU_ITEM_SEQ"].ToString());
                        info.Name = Convert.IsDBNull(dr[i]["MENU_ITEM_NAME"]) ? "" : (dr[i]["MENU_ITEM_NAME"]).ToString();
                        info.UpperSeq = Convert.IsDBNull(dr[i]["UPPER_MENU_ITEM_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["UPPER_MENU_ITEM_SEQ"].ToString());

                        infoList.Add(info);
                    }
                }
          

            }
            catch (Exception ex)
            {
                throw (ex);
            
            }


            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }
    }
}
